<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>js面试题</title>
    <link rel="stylesheet" href="../1style/index.css"/>
    <script src="../1style/jquery.js"></script>
</head>
<body>
<h1>JS面试题笔试题</h1>


<h3>以下打印出什么? node 环境下 window 没这个对象</h3>
<div>
    <pre>
        var a=10;
        function test(){
            a=100;
            console.log(a);  // ?
            console.log(this.a); // ?
            var a;
            console.log(a); // ?
        }
        test(); // https://www.bilibili.com/video/BV1oJ411473Y?p=7 //企业常问面试题一
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        (function (){
            var a=b=3
        })()
        console.log(a)
        console.log(b)
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        for (var i=1; i<=3; i++){
            setTimeout(function(){
                console.log(i)
            },0)
        }
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        function fun(n){
            console.log(n);
            var n=456;
            console.log(n)
        }
        var n = 123;
        fun(n);
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        function func(){
            console.log(fun);
            fun=456;
            console.log(fun)
        }
        func();
        var fun= 123;

    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
      var n = 123;
      function f1() {
          console.log(n)
      }
      function f2() {
          var n = 456;
          f1();
      }
      f2();
      console.log(n)
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
      var length = 100;
      function f1() {
          console.log(this.length);
      }
      var obj = {
          x: 10,
          f2: function (f1) {
              f1();
              arguments[0]();
          }
      };
      obj.f2(f1, 1)
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        function f(){
            console.log(this.a);
        }
        var obj = { a: 2, f:f };
        var f2 = obj.f;
        var a = 'hello';
        f2();               // f2() ; obj.f()
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>

        function f(s){
            console.log(this.a, s);
            return this.a + s;
        }

        var obj = { a: 2 };

        var f2 = function (){
            return f.apply(obj, arguments)
        }

        var b = f2(3);
        console.log(b);
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        function b(){
            console.log(a);
            var a = 10;
            function a(){};
            a = 100;
            console.log(a)
        }
        b();
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        (function d(num){
            console.log(num);
            var num = 10;
        })(100)
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        (function d(num){
            console.log(num);
            var num = 10;
            function num(){}
        })(100)
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        function m(){
            console.log(a1);
            console.log(a2);
            console.log(b1);
            console.log(b2);
            if(false){
                function b1(){};
                var a1 = 100;
            }
            if(true){
                function b2(){};
                var a2 = 10;
            }
            console.log(a1);
            console.log(a2);
            console.log(b1);
            console.log(b2);
        }
        m();
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        (function f(num){
            function num(){}
            console.log(num);
            var num = 10;
            console.log(num);
        })(100)
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        function n(){
            if(2>1){
                arr = 10;
                brr = 10;
                let arr;
                var brr;
                console.log(arr);
                console.log(brr);
            }
        };
        n();
    </pre>
</div>

<h3>以下打印出什么? </h3>
<div>
    <pre>
        var R = (function(){
            var u = {a:1, b:2};
            var r = {
                fn: function (k){
                    return u[k]
                }
            }
            return r;
        })()
        console.log(R.fn('a'));
    </pre>
</div>
<h3>以下打印出什么? </h3>
<div>
    <pre>
        var num1=55;
        var num2=66;
        function f1(num, num1){
            num = 100;
            num1 = 100;
            num2 = 100;
            console.log(num,num1,num2);
        }
        f1(num1,num2);
        console.log(num1);
        console.log(num2);
        console.log(num);
    </pre>
</div>
<h3>以下打印出什么? </h3>
<div>
    <pre>
        function Person (name, age, salary){
            this.name = name;
            this.age = age;
            this.salary = salary;
        }

        function f1 (person){
            person.name = 'ls';
            person = new Person('aa', 18, 10);
        }

        var p = new Person('zs', 18 ,1000);
        console.log(p.name);
        f1(p);
        console.log(p.name);
    </pre>
</div>
<h3>以下打印出什么? </h3>
<div>
    <pre>
    let a = {}, b = "0", c = 0;
    a[b] = '1';
    a[c] = 2;
    console.log(a[b]);
    ///////////////////////////////////
    let a = {}, b = Symbol('1'), c = Symbol('1');
    a[b] = '1';
    a[c] = 2;
    console.log(a[b]);
    ///////////////////////////////////////
    let a = {}, b = {n: 1}, c = {s: 2};
    a[b] = '1';
    a[c] = 2;
    console.log(a[b]);
    </pre>
</div>
<h3>以下打印出什么? </h3>
<div>
    <pre>

    function Foo() {
        getName = function () {
            console.log(1);
        };
        return this;
    }
    Foo.getName = function () {
        console.log(2);
    };
    Foo.prototype.getName = function () {
        console.log(3);
    };
    var getName = function () {
        console.log(4);
    };
    function getName() {
        console.log(5);
    }
    I
    Foo.getName(); //
    getName(); //
    Foo().getName(); //
    getName(); //
    new Foo.getName(); //
    new Foo().getName(); //
    new new Foo().getName();
    </pre>
</div>
<h3>以下打印出什么? </h3>
<div>
    <pre>

    async function async1() {
        console.log('async1 start');
        await async2();
        console.log('async1 end');
    }

    async function async2() {
        console.log('async2');
    }

    console.log('script start');
    setTimeout(function () {
        console.log(' setTimeout');
    }, 0)
    async1();
    new Promise(function (resolve) {
        console.log(' promise1');
        resolve();
    }).then(function () {
        console.log(' promise2');
    });
    console.log('script end');

    </pre>
</div>
<h3>以下打印出什么? </h3>
<div>
    <pre>

    function A() {
        alert(1);
    }

    function Func() {
        A = function () {
            alert(2);
        };
        return this;
    }

    Func.A = A;
    Func.prototype = {
        A: () => {
            alert(3);
        }
    };
    A();
    Func.A();
    Func().A();
    new Fn.A();
    new Fn().A();
    new new Fn().A();


    </pre>
</div>

</body>
</html>

<script>
  $(function () {
    $("h3").click(function () {
      $(this).next().toggle();
    });

    for (let i=0; i<5;i++){
        setTimeout(function () {
            console.log(i)
        },1000)
    }

  })
</script>

